<template>
  <div class="order-management">
    <!-- 统计面板 -->
    <div class="stats-panel">
      <div class="stat-item">
        <h3>今日订单</h3>
        <p class="number">{{ stats.todayOrders }}</p>
      </div>
      <div class="stat-item">
        <h3>进行中</h3>
        <p class="number processing">{{ stats.processingOrders }}</p>
      </div>
      <div class="stat-item">
        <h3>待处理</h3>
        <p class="number pending">{{ stats.pendingOrders }}</p>
      </div>
    </div>

    <!-- 订单筛选 -->
    <el-row style="margin-bottom: 20px;">
      <el-col
        :xs="24"
        :sm="12"
        style="text-align: left;"
      >
        <el-radio-group v-model="activeStatus">
          <el-radio-button value="all">全部</el-radio-button>
          <el-radio-button value="pending">待处理</el-radio-button>
          <el-radio-button value="processing">进行中</el-radio-button>
          <el-radio-button value="completed">已完成</el-radio-button>
        </el-radio-group>
      </el-col>
      <el-col
        :xs="24"
        :sm="12"
        style="text-align: right;margin-top: 20px;"
      >
        <el-button
          @click="refreshOrders"
          class="refresh-btn"
        >
          刷新订单
        </el-button>
      </el-col>
    </el-row>

    <!-- 订单列表 -->
    <div class="order-list">
      <transition-group name="list">
        <div
          v-for="order in filteredOrders"
          :key="order.id"
          :class="['order-card', `status-${order.status}`]"
        >
          <div class="order-header">
            <div class="order-meta">
              <span class="order-id">#{{ order.id }}</span>
              <span class="order-time">{{ formatTime(order.createdAt) }}</span>
            </div>
            <div class="order-status">
              {{ statusMap[order.status] }}
            </div>
          </div>

          <div class="order-body">
            <div class="items-list">
              <div
                v-for="item in order.items"
                :key="item.id"
                class="item"
              >
                <span class="item-name">{{ item.name }}</span>
                <span class="item-quantity">×{{ item.quantity }}</span>
                <span
                  class="item-note"
                  v-if="item.note"
                >备注：{{ item.note }}</span>
              </div>
            </div>

            <div class="order-footer">
              <div class="total-price">
                总价：¥{{ order.total.toFixed(2) }}
              </div>
              <div class="actions">
                <el-button
                  v-if="order.status === 'pending'"
                  type="primary"
                  size="small"
                  @click="acceptOrder(order)"
                >
                  接单
                </el-button>
                <el-button
                  v-if="order.status === 'processing'"
                  type="success"
                  size="small"
                  @click="completeOrder(order)"
                >
                  完成
                </el-button>
                <el-button
                  type="danger"
                  size="small"
                  @click="cancelOrder(order)"
                >
                  取消
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </transition-group>

      <div
        v-if="filteredOrders.length === 0"
        class="empty"
      >
        暂无相关订单
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

// 模拟数据
const orders = ref([
  {
    id: '1001',
    status: 'pending',
    createdAt: new Date(),
    items: [
      { id: 1, name: '红烧肉', quantity: 2, price: 28, note: '少油少盐' },
      { id: 2, name: '清蒸鱼', quantity: 1, price: 35 }
    ],
    total: 91
  },
  // 更多测试数据...
]);

const activeStatus = ref('all');
const stats = ref({
  todayOrders: 0,
  pendingOrders: 0,
  processingOrders: 0
});

const statusMap = {
  pending: '待处理',
  processing: '进行中',
  completed: '已完成',
  canceled: '已取消'
};

// 计算属性
const filteredOrders = computed(() => {
  return orders.value.filter(order => {
    if (activeStatus.value === 'all') return true;
    return order.status === activeStatus.value;
  }).sort((a, b) => b.createdAt - a.createdAt);
});

// 方法
const formatTime = (date) => {
  return new Date(date).toLocaleTimeString('zh-CN', {
    hour: '2-digit',
    minute: '2-digit'
  });
};

const acceptOrder = async (order) => {
  try {
    order.status = 'processing';
    ElMessage.success('接单成功');
  } catch (error) {
    ElMessage.error('操作失败');
  }
};

const completeOrder = async (order) => {
  try {
    order.status = 'completed';
    ElMessage.success('订单已完成');
  } catch (error) {
    ElMessage.error('操作失败');
  }
};

const cancelOrder = (order) => {
  ElMessageBox.confirm('确定要取消此订单吗？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    order.status = 'canceled';
    ElMessage.success('订单已取消');
  });
};

const refreshOrders = async () => {
  // 这里可以添加API调用
  ElMessage.success('已刷新订单列表');
};

const updateStats = () => {
  stats.value = {
    todayOrders: orders.value.length,
    pendingOrders: orders.value.filter(o => o.status === 'pending').length,
    processingOrders: orders.value.filter(o => o.status === 'processing').length
  };
};

// 初始化
onMounted(() => {
  setInterval(updateStats, 1000);
})
</script>

<style scoped>
.order-management {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.stats-panel {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 30px;
}

.stat-item {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.stat-item h3 {
  margin: 0 0 10px;
  color: #666;
}

.stat-item .number {
  font-size: 28px;
  margin: 0;
  font-weight: bold;
}

.stat-item .pending {
  color: #e6a23c;
}
.stat-item .processing {
  color: #409eff;
}

.filters {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.refresh-btn {
  margin-left: auto;
}

.order-list {
  display: grid;
  gap: 15px;
}

.order-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.order-card.status-pending {
  border-left: 4px solid #e6a23c;
}

.order-card.status-processing {
  border-left: 4px solid #409eff;
}

.order-card.status-completed {
  border-left: 4px solid #67c23a;
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.order-meta {
  display: flex;
  flex-direction: column;
}

.order-id {
  font-weight: bold;
  margin-bottom: 5px;
}

.order-time {
  color: #999;
  font-size: 0.9em;
}

.order-status {
  font-weight: bold;
  color: #409eff;
}

.items-list {
  margin-bottom: 15px;
}

.item {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px dashed #eee;
}

.item:last-child {
  border-bottom: none;
}

.item-note {
  color: #999;
  font-size: 0.9em;
  margin-left: 10px;
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
  padding-top: 10px;
}

.total-price {
  font-weight: bold;
  color: #e6a23c;
}

.actions {
  display: flex;
  gap: 10px;
}

.empty {
  text-align: center;
  color: #999;
  padding: 40px 0;
}

.list-enter-active,
.list-leave-active {
  transition: all 0.5s ease;
}
.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

@media (max-width: 768px) {
  .stats-panel {
    grid-template-columns: 1fr;
  }

  .order-card {
    padding: 15px;
  }

  .order-footer {
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
  }
}
</style>